package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.StrongholdActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_GENERAL_ACTIVITY_QUERY_MEMBER_CONDITION, desc = "跨服活动商会成员贡献值")
public class CrossGeneralActivityQueryMemberConditionCmd extends AbstractCommand {

    private int activityId;

    private String unionUid;

    private long serverId;        //发起查询的区服编号

    private long userId;          //发起查询的玩家编号

    private int conditionType;

    private UnionActivityConditionData unionActivityConditionData;

    private StrongholdActivityProto.CrossStrongholdActivityConditionUnionMemberDataReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = StrongholdActivityProto.CrossStrongholdActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        unionUid = reqMsg.getUnionUid();
        serverId = reqMsg.getServerId();
        userId = reqMsg.getUserId();
        conditionType = reqMsg.getConditionType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (null == activityInfo) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        unionActivityConditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, conditionType, unionUid);
        if (null == unionActivityConditionData) {
            ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
            return;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdActivityProto.CrossStrongholdConditionUnionMemberDataRespMsg2.Builder respMsg = StrongholdActivityProto.CrossStrongholdConditionUnionMemberDataRespMsg2.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setRet(ret);
        respMsg.setServerId(serverId);
        respMsg.setUserId(userId);
        if (0 == ret) {
            if (null != unionActivityConditionData) {
                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityPb.parseActivityConditionUnionMemberDataListMsg(
                        UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, GeneralActivityRankMgr.getUserActivityConditionDataMap(activityId, conditionType)));
                respMsg.setDataList(dataMsg);
            }
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_GENERAL_MEMBER_CONDITION_DATA_RESP, respMsg);
        GamePlayerMgr.sendPacket(0, pbMsg);
    }

}
